package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //对密码进行加密(用用户名作为盐
        Md5Hash md5Hash = new Md5Hash(employee.getPassword(), employee.getName());
        employee.setPassword(md5Hash.toString());

        employeeMapper.insert(employee);
        if (ids != null && ids.length > 0) {
            employeeMapper.insertRelation(employee.getId(), ids);
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        //删除原有的关系
        employeeMapper.deleteRelation(employee.getId());
        if (ids != null && ids.length > 0) {
            employeeMapper.insertRelation(employee.getId(), ids);
        }
        //处理中间表
        if (ids != null && ids.length > 0) {
            employeeMapper.insertRelation(employee.getId(), ids);
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.queryForList(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public Employee login(String username, String password) {
        if (username.equals("") && username == null) {
            throw new RuntimeException("用户名不能为空");
        }
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            throw new RuntimeException("用户名和密码不匹配");
        }
        return employee;
    }

    @Override
    public void updatePassword(String newPassword, Long employeeId) {
        employeeMapper.updatePassword(newPassword, employeeId);
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.deleteByIds(ids);
    }

    @Override
    public Employee checkEmployeeById(Long id) {
        Employee employee = employeeMapper.checkEmployeeByid(id);
        return employee;
    }

    @Override
    public Employee checkEmployeeByName(String name) {
        Employee employee = employeeMapper.checkEmployeeByName(name);
        return employee;
    }

    @Override
    public void changeEnableState(Long empId) {
        Employee employee = employeeMapper.selectByPrimaryKey(empId);
        if (employee.isEnablestate()) {
            employeeMapper.changeEnableState(empId,0);
        }else {
            employeeMapper.changeEnableState(empId,1);
        }

    }

    @Override
    public List<Employee> selectByRoleSn(String... sns) {
         List<Employee> list=employeeMapper.selectByRoleSn(sns);
        return list;
    }

    @Override
    public HSSFWorkbook exportXls() {
        //获取所有员工
        List<Employee> employees = employeeMapper.selectAll();
        //创建一个EXCEL
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个sheet
        HSSFSheet sheet = workbook.createSheet("员工通讯录");
        //创建标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        for (int i = 0; i < employees.size(); i++) {
            //获取每一个员工
            Employee employee = employees.get(i);
            //创建行
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            if (employee.getAge() != null) {
                row.createCell(2).setCellValue(employee.getAge());
            }
        }

        return workbook;
    }

    @Override
    public void importXls(HSSFWorkbook workbook) {
        //获取表格第一页数据
        HSSFSheet sheetAt = workbook.getSheetAt(0);
        //获取最后一行的行数
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 1;i<=lastRowNum;i++){
            HSSFRow row = sheetAt.getRow(i);
            Employee employee = new Employee();
            String name=row.getCell(0).getStringCellValue();
            if (name==null){
                continue;
            }
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());
            employee.setPassword("1");

            HSSFCell cell = row.getCell(2);
            if (cell.getCellType().equals(CellType.STRING)){
                String value = row.getCell(2).getStringCellValue();
                //年龄列按照文本类型来读取
                employee.setAge(Integer.valueOf(value));
            }
            else {
                double value = row.getCell(2).getNumericCellValue();
                employee.setAge((int) value);
            }
            save(employee,null);
        }

        //获取行

    }
}
